import http from "@/api/config/http";
import { fileTemplate } from "../utils";
import { Tag } from "./types";

import { IQuery, IResponse, ITableResponse } from "@/interface/IAxios";
import { IResolution } from "../resolution/types";

// 查询价签管理列表
export function listPriceTag(query?: IQuery): Promise<ITableResponse<Tag>> {
  query = JSON.parse(JSON.stringify(query));

  if (query) {
    query.mType = query.mType.join(",");
  }
  return http.request({
    url: "/priceTag/priceTag/list",
    method: "get",
    params: query
  });
}
// 删除价签管理
export function delPriceTag(tagId: Array<string>): Promise<IResponse<Tag>> {
  return http.request({
    url: `/priceTag/priceTag/${tagId}`,
    method: "delete"
  });
}
// 修改价签管理
export function updatePriceTag(data: Partial<Tag>) {
  return http.request({
    url: "/priceTag/priceTag",
    method: "put",
    data
  });
}
// 新增价签管理
export function addPriceTag(data: Tag) {
  return http.request({
    url: "/priceTag/priceTag",
    method: "post",
    data
  });
}

type Firmware = {
  modelId: string;
  version: string;
};

type VersionType = {
  tags: string[];
  firmware: Firmware;
};
/* 硬件升级 */
export function upgradeEslVersion(data: VersionType) {
  return http.request({
    url: "/firmware/firmware/upgrade",
    method: "post",
    data
  });
}
/* 硬件升级 */
export function upgradeFirmware(data: { tags: Array<string> }) {
  return http.request({
    url: "/firmware/firmware/upgrade",
    method: "post",
    data
  });
}
/* 硬件升级 */
export function listFw(modelId: string) {
  return http.request({
    url: `/firmware/firmware/listFw`,
    method: "get",
    params: {
      modelId
    }
  });
}

// 亮灯
export function light(data: { times: number; color: string; tags: string[] }) {
  return http.request({
    url: "priceTag/priceTag/light",
    method: "post",
    data
  });
}
// 电量,battery
export function powerState() {
  return http.request({
    url: "priceTag/priceTag/powerState",
    method: "get"
  });
}

// 批量绑定模板接口
export function downloadTemplate(url: string) {
  return http.request({
    url,
    method: "get"
  });
}

export const downloadFile = () => {
  return fileTemplate("/priceTag/priceTag/export");
};
export const manifestFile = () => {
  return fileTemplate("/priceTag/priceTag/manifest?scope=0");
};

export const transportPart = (to: string, list?: string[]) => {
  return http.request({
    url: "/priceTag/priceTag/shift",
    method: "post",
    data: {
      list,
      to
    }
  });
};

export const transportAll = (to: string, from: string) => {
  return http.request({
    url: "/priceTag/priceTag/shift",
    method: "get",
    params: {
      to,
      from
    }
  });
};
export const relations = () => {
  return fileTemplate("/priceTag/priceTag/relations?scope=0");
};

// 查询商品信息列表

export function listResolution(query?: IQuery): Promise<ITableResponse<IResolution>> {
  return http.request({
    url: "/resolution/resolution/list",
    method: "get",
    params: query
  });
}

export function queryResolution(): Promise<IResponse<IResolution[]>> {
  return http.request({
    url: "/resolution/resolution/query",
    method: "get"
  });
}

// 保存下发
export function saveAndSend(data: Partial<Tag> & { goodsList: any[] }) {
  return http.request({
    url: "/priceTag/priceTag/saveAndSend",
    method: "put",
    data
  });
}

// 预览图片
export function preview(params: { goodsList: any[]; templateId?: string; tagCode: string }) {
  return http.request({
    url: "/priceTag/priceTag/preview",
    method: "post",
    data: params
  });
}

export const priceTagRecover = (list: string[]) => {
  return http.request({
    url: "/priceTag/priceTag/recover",
    method: "post",
    data: list
  });
};
